home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / fortran / pgplot5.1 / pgplot5 / pgplot5.1.0 / examples-src / cpgdemo.c next >
Encoding:
C/C++ Source or Header  |  1996-02-12  |  4.4 KB  |  191 lines

  1. #include "cpgplot.h"
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6.  
  7. #ifndef EXIT_FAILURE
  8. #define EXIT_FAILURE 1
  9. #endif
  10. #ifndef EXIT_SUCCESS
  11. #define EXIT_SUCCESS 0
  12. #endif
  13.  
  14. static void demo1();
  15. static void demo2();
  16. static void demo3();
  17.  
  18. /* ---------------------------------------------------------------------
  19.  * Demonstration program for PGPLOT called from C.
  20.  * (Note that conventions for calling Fortran from C and C from FORTRAN
  21.  * are system-dependent).
  22.  * Usage:
  23.  *    cc -c cpgdemo.c
  24.  *    f77 -o cpgdemo cpgdemo.o -lcpgplot -lpgplot -lX11
  25.  *----------------------------------------------------------------------
  26.  */
  27.  
  28. int main()
  29. {
  30.   /*
  31.    * Call ppgbeg to initiate PGPLOT and open the output device; cpgbeg
  32.    * will prompt the user to supply the device name and type.
  33.    */
  34.   if(cpgbeg(0, "?", 1, 1) != 1)
  35.     exit(EXIT_FAILURE);
  36.   cpgask(1);
  37.   /*
  38.    * Call each demo.
  39.    */
  40.   demo1();
  41.   demo2();
  42.   demo3();
  43.   /*
  44.    * Finally, call cpgend to terminate things properly.
  45.    */
  46.   cpgend();
  47.   return EXIT_SUCCESS;
  48. }
  49.  
  50. static void demo1()
  51. {
  52.   int i;
  53.   static float xs[] = {1.0, 2.0, 3.0, 4.0, 5.0 };
  54.   static float ys[] = {1.0, 4.0, 9.0, 16.0, 25.0 };
  55.   float xr[60], yr[60];
  56.   int n = sizeof(xr) / sizeof(xr[0]);
  57.   /*
  58.    * Call cpgenv to specify the range of the axes and to draw a box, and
  59.    * cpglab to label it. The x-axis runs from 0 to 10, and y from 0 to 20.
  60.    */
  61.   cpgenv(0.0, 10.0, 0.0, 20.0, 0, 1);
  62.   cpglab("(x)", "(y)", "PGPLOT Example 1: y = x\\u2\\d");
  63.   /*
  64.    * Mark five points (coordinates in arrays XS and YS), using symbol
  65.    * number 9.
  66.    */
  67.   cpgpt(5, xs, ys, 9);
  68.   /*
  69.    * Compute the function at 'n=60' points, and use cpgline to draw it.
  70.    */
  71.   for(i=0; i<n; i++) {
  72.     xr[i] = 0.1*i;
  73.     yr[i] = xr[i]*xr[i];
  74.   }
  75.   cpgline(n, xr, yr);
  76.   return;
  77. }
  78.  
  79. /*----------------------------------------------------------------------
  80.  * Demonstration function for PGPLOT contouring routines.
  81.  *--------------------------------------------------------------------*/
  82.  
  83. static void demo2()
  84. {
  85.   static int nx = 40, ny = 40;
  86.   int i, j, k, lw, ci, ls;
  87.   float f[1600], fmin, fmax, alev;
  88.   double x, y;
  89.   static float tr[6] = {0.0, 1.0, 0.0, 0.0, 0.0, 1.0};
  90.   
  91.   /* Compute a suitable function. A C array is used to emulate
  92.      a 2D fortran array f(nx,ny). */
  93.  
  94.   fmin = fmax = 0.0;
  95.   for (j=1; j<=ny; j++) {
  96.     for (i=1; i<=ny; i++) {
  97.       k = (j-1)*nx + (i-1);    /* Fortran convention */
  98.       x = tr[0] + tr[1]*i + tr[2]*j;
  99.       y = tr[3] + tr[4]*i + tr[5]*j;
  100.       f[k] = cos(0.3*sqrt(x*2)-0.13333*y)*cos(0.13333*x)+
  101.     (x-y)/(double)nx;
  102.       if (f[k] < fmin) fmin = f[k];
  103.       if (f[k] > fmax) fmax = f[k];
  104.     }
  105.   }
  106.   
  107.   /* Clear the screen. Set up window and viewport. */
  108.   
  109.   cpgpage();
  110.   cpgsvp(0.05, 0.95, 0.05, 0.95);
  111.   cpgswin(1.0, (float) nx, 1.0, (float) ny);
  112.   cpgbox("bcts", 0.0, 0, "bcts", 0.0, 0);
  113.   cpgmtxt("t", 1.0, 0.0, 0.0, "Contouring using cpgcont()");
  114.   
  115.   /* Draw the map. cpgcont is called once for each contour, using
  116.      different line attributes to distinguish contour levels. */
  117.   
  118.   cpgbbuf();
  119.   for (i=1; i<21; i++) {
  120.     alev = fmin + i*(fmax-fmin)/20.0;
  121.     lw = (i%5 == 0) ? 3 : 1;
  122.     ci = (i < 10)   ? 2 : 3;
  123.     ls = (i < 10)   ? 2 : 1;
  124.     cpgslw(lw);
  125.     cpgsci(ci);
  126.     cpgsls(ls);
  127.     cpgcont(f, nx, ny, 1, nx, 1, ny, &alev, -1, tr);
  128.   }
  129.   cpgslw(1);
  130.   cpgsls(1);
  131.   cpgsci(1);
  132.   cpgebuf();
  133.   return;
  134. }
  135.  
  136. static void demo3()
  137. {
  138. #define TWOPI (2.0*3.14159265)
  139. #define NPOL  6
  140.   
  141.   int i, j, k;
  142.   int n1[] = {3, 4, 5, 5, 6, 8};
  143.   int n2[] = {1, 1, 1, 2, 1, 3};
  144.   float x[10], y[10], y0;
  145.   
  146.   char* lab[] =  {"Fill style 1 (solid)",
  147.           "Fill style 2 (outline)",
  148.           "Fill style 3 (hatched)",
  149.           "Fill style 4 (cross-hatched)"};
  150.   
  151. /* Initialize the viewport and window. */
  152.  
  153.   cpgbbuf();
  154.   cpgsave();
  155.   cpgpage();
  156.   cpgsvp(0.0, 1.0, 0.0, 1.0);
  157.   cpgwnad(0.0, 10.0, 0.0, 10.0);
  158.   
  159. /* Label the graph. */
  160.  
  161.   cpgsci(1);
  162.   cpgmtxt("T", -2.0, 0.5, 0.5, 
  163.           "PGPLOT fill area: routines cpgpoly(), cpgcirc(), cpgrect()");
  164.   
  165. /* Draw assorted polygons. */
  166.  
  167.   for (k=1; k<5; k++) {
  168.     cpgsci(1);
  169.     y0 = 10.0 -2.0*k;
  170.     cpgtext(0.2, y0+0.6, lab[k-1]);
  171.     cpgsfs(k);
  172.     for (i=0; i<NPOL; i++) {
  173.       cpgsci(i+1);
  174.       for (j=0; j<n1[i]; j++) {
  175.     x[j] = i+1 + 0.5*cos(n2[i]*TWOPI*j/n1[i]);
  176.     y[j] = y0 + 0.5*sin(n2[i]*TWOPI*j/n1[i]);
  177.       }
  178.       cpgpoly(n1[i], x, y);
  179.     }
  180.     cpgsci(7);
  181.     cpgshs(0.0, 1.0, 0.0);
  182.     cpgcirc(7.0, y0, 0.5);
  183.     cpgsci(8);
  184.     cpgshs(-45.0, 1.0, 0.0);
  185.     cpgrect(7.8, 9.5, y0-0.5, y0+0.5);
  186.   }
  187.   cpgunsa();
  188.   cpgebuf();
  189.   return;
  190. }
  191.